// router/articleModel.js
const Router = require("koa-router");
const {
  ArticleManager,
} = require("../../mongodb/manager/article/articleModel");
const { verifyToken } = require("../../middleware");
const router = new Router();

/**
 * @swagger
 * tags:
 *   name: Article
 *   description: 文章接口
 */

// 创建文章接口
/**
 * @swagger
 * /articles/:
 *   post:
 *     summary: 创建文章
 *     tags: [Article]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               title:
 *                 type: string
 *                 example: "我的第一篇文章"
 *               content:
 *                 type: string
 *                 example: "这是一篇关于我的第一篇文章的内容。"
 *               sort:
 *                 type: string
 *                 example: "动态"
 *               origin:
 *                 type: string
 *                 example: "个人"
 *               town:
 *                 type: string
 *                 example: "阳光小镇"
 *     responses:
 *       200:
 *         description: 文章创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 title:
 *                   type: string
 *                   example: "我的第一篇文章"
 *                 content:
 *                   type: string
 *                   example: "这是一篇关于我的第一篇文章的内容。"
 *                 sort:
 *                   type: string
 *                   example: "动态"
 *                 origin:
 *                   type: string
 *                   example: "个人"
 *                 town:
 *                   type: string
 *                   example: "北京"
 *       400:
 *         description: 请求参数错误
 */
router.post("/", verifyToken("admin"), async (ctx) => {
  try {
    const article = await ArticleManager.createArticle(ctx.request.body);
    ctx.status = 200;
    ctx.body = article;
  } catch (error) {
    ctx.status = 400;
    ctx.body = { message: error.message };
  }
});

// 修改文章接口
/**
 * @swagger
 * /articles/:
 *   put:
 *     summary: 修改文章
 *     tags: [Article]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               title:
 *                 type: string
 *                 example: "我的第一篇文章"
 *               updateData:
 *                 type: object
 *                 additionalProperties: true
 *                 example:
 *                   content: "这是一篇更新后的内容。"
 *     responses:
 *       200:
 *         description: 文章修改成功
 *       400:
 *         description: 请求参数错误
 *       404:
 *         description: 文章未找到
 */
router.put("/", verifyToken("admin"), async (ctx) => {
  const { title } = ctx.request.body; // 查询字段
  const { updateData } = ctx.request.body; // 更新字段

  let article;

  // 查找
  if (title) {
    article = await ArticleManager.getArticleByTitle(title);
  } else {
    ctx.status = 400;
    ctx.body = { message: "Title is required." };
    return;
  }

  // 更新
  try {
    const updateArticle = await ArticleManager.updateArticle(
      article._id,
      updateData,
    );
    ctx.status = 200;
    ctx.body = { message: "修改成功", updateArticle };
  } catch (error) {
    ctx.status = 404;
    ctx.body = { message: error.message };
  }
});

// 删除文章接口
/**
 * @swagger
 * /articles/:
 *   delete:
 *     summary: 删除文章
 *     tags: [Article]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               title:
 *                 type: string
 *                 example: "我的第一篇文章"
 *     responses:
 *       200:
 *         description: 文章成功标记为删除
 *       400:
 *         description: 请求参数错误
 *       404:
 *         description: 文章未找到
 */
router.delete("/", verifyToken("admin"), async (ctx) => {
  const { title } = ctx.request.body;

  // 检查请求体
  if (!title) {
    ctx.status = 400;
    ctx.body = { message: "Title is required." };
    return;
  }

  let article;

  if (title) {
    article = await ArticleManager.getArticleByTitle(title);
  }

  // 如果不存在，返回错误
  if (!article) {
    ctx.status = 404;
    ctx.body = {
      message: "Article not found",
    };
    return;
  }

  // 使用已存在的标记删除方法
  try {
    await ArticleManager.softDeleteUser(article._id);
    ctx.status = 200;
    ctx.body = { message: "文章成功标记为删除。" };
  } catch (error) {
    ctx.status = 500;
    ctx.body = { message: "删除过程中出现错误。" };
  }
});

// 查找所有文章列表接口
/**
 * @swagger
 * /articles/:
 *   get:
 *     summary: 查找所有文章列表
 *     tags: [Article]
 *     responses:
 *       200:
 *         description: 返回所有活跃的文章
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 type: object
 *                 properties:
 *                   title:
 *                     type: string
 *                     example: "我的第一篇文章"
 *                   content:
 *                     type: string
 *                     example: "这是一篇关于我的第一篇文章的内容。"
 *                   sort:
 *                     type: string
 *                     example: "动态"
 *                   origin:
 *                     type: string
 *                     example: "个人"
 *                   town:
 *                     type: string
 *                     example: "北京"
 *       500:
 *         description: 服务器错误
 */
router.get("/", async (ctx) => {
  try {
    const articles = await ArticleManager.getAllActiveArticles();
    ctx.status = 200;
    ctx.body = articles;
  } catch (error) {
    ctx.status = 500;
    ctx.body = { message: error.message };
  }
});

// 修改文章发布状态接口
/**
 * @swagger
 * /articles/publish-status:
 *   put:
 *     summary: 修改文章发布状态
 *     tags: [Article]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               title:
 *                 type: string
 *                 example: "我的第一篇文章"
 *               isPublished:
 *                 type: boolean
 *                 example: true
 *     responses:
 *       200:
 *         description: 发布状态更新成功
 *       400:
 *         description: 请求参数错误
 *       404:
 *         description: 文章未找到
 */
router.put("/publish-status", verifyToken("admin"), async (ctx) => {
  const { title, isPublished } = ctx.request.body; // 获取请求参数

  if (!title || typeof isPublished !== "boolean") {
    ctx.status = 400;
    ctx.body = {
      message: "失败。",
    };
    return;
  }

  try {
    const article = await ArticleManager.getArticleByTitle(title);
    if (!article) {
      ctx.status = 404;
      ctx.body = { message: "文章未找到" };
      return;
    }

    // 更新发布状态
    article.isPublished = isPublished;
    await article.save();

    ctx.status = 200;
    ctx.body = { message: "发布状态更新成功", article };
  } catch (error) {
    ctx.status = 500;
    ctx.body = { message: error.message };
  }
});

// 根据 town 和 sort 获取文章
/**
 * @swagger
 * /articles/filter:
 *   get:
 *     summary: 根据 town 和 sort 获取文章
 *     tags: [Article]
 *     parameters:
 *       - name: town
 *         in: query
 *         required: true
 *         schema:
 *           type: string
 *           example: "阳光小镇"
 *       - name: sort
 *         in: query
 *         required: false
 *         schema:
 *           type: string
 *           example: "动态"
 *     responses:
 *       200:
 *         description: 返回符合条件的文章
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 type: object
 *                 properties:
 *                   title:
 *                     type: string
 *                     example: "我的第一篇文章"
 *                   content:
 *                     type: string
 *                     example: "这是一篇关于我的第一篇文章的内容。"
 *                   sort:
 *                     type: string
 *                     example: "动态"
 *                   origin:
 *                     type: string
 *                     example: "个人"
 *                   town:
 *                     type: string
 *                     example: "阳光小镇"
 *       400:
 *         description: 请求参数错误
 *       500:
 *         description: 服务器错误
 */
router.get("/filter", async (ctx) => {
  const { town, sort } = ctx.query;

  // 校验参数
  if (!town) {
    ctx.status = 400;
    ctx.body = { message: "Town 参数是必须的" };
    return;
  }

  try {
    const articles = await ArticleManager.getArticlesByTownAndSort(town, sort);
    ctx.status = 200;
    ctx.body = articles;
  } catch (error) {
    ctx.status = 500;
    ctx.body = { message: error.message };
  }
});

// 根据文章 ID 获取文章接口
/**
 * @swagger
 * /articles/{id}:
 *   get:
 *     summary: 根据文章 ID 获取文章
 *     tags: [Article]
 *     parameters:
 *       - name: id
 *         in: path
 *         required: true
 *         schema:
 *           type: string
 *           example: "60d4eaeff5b1e0f4a50756ef"
 *     responses:
 *       200:
 *         description: 返回文章信息
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 title:
 *                   type: string
 *                   example: "我的第一篇文章"
 *                 content:
 *                   type: string
 *                   example: "这是一篇关于我的第一篇文章的内容。"
 *                 sort:
 *                   type: string
 *                   example: "动态"
 *                 origin:
 *                   type: string
 *                   example: "个人"
 *                 town:
 *                   type: string
 *                   example: "北京"
 *       400:
 *         description: 请求参数错误
 *       404:
 *         description: 文章未找到
 *       500:
 *         description: 服务器错误
 */
router.get("/:id", async (ctx) => {
  const { id } = ctx.params;

  // 校验 ID 参数
  if (!id) {
    ctx.status = 400;
    ctx.body = { message: "Article ID is required." };
    return;
  }

  try {
    const article = await ArticleManager.getArticleById(id);

    // 如果没有找到文章
    if (!article) {
      ctx.status = 404;
      ctx.body = { message: "Article not found." };
      return;
    }

    // 返回文章信息
    ctx.status = 200;
    ctx.body = article;
  } catch (error) {
    ctx.status = 500;
    ctx.body = { message: error.message };
  }
});

module.exports = { router };
